import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { ConfigProvider, App } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import { AuthProvider } from './contexts/AuthContext';
import PrivateRoute from './components/Guards/PrivateRoute';
import PublicRoute from './components/Guards/PublicRoute';
import MainLayout from './components/Layout/MainLayout';
import Home from './pages/Home';
import Login from './pages/Auth/Login';
import Register from './pages/Auth/Register';
import Courses from './pages/Courses';
import CourseDetail from './pages/Courses/Detail';
import Profile from './pages/Profile';
import Dashboard from './pages/Dashboard';
import StudentManagement from './pages/StudentManagement';
import LessonReplay from './pages/LessonReplay';
import MyCourses from './pages/MyCourses';
// 全局样式已在 main.jsx 中导入

// 配置Ant Design主题
const theme = {
  token: {
    colorPrimary: '#1890ff',
    borderRadius: 6,
    fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif',
  },
};

function Main() {
  return (
    <ConfigProvider locale={zhCN} theme={theme}>
      <App>
        <AuthProvider>
          <Router>
            <div className="app">
              <Routes>
                {/* 公开路由 - 未登录可访问 */}
                <Route path="/login" element={
                  <PublicRoute>
                    <Login />
                  </PublicRoute>
                } />
                {/* <Route path="/register" element={
                  <PublicRoute>
                    <Register />
                  </PublicRoute>
                } /> */}
                
                {/* 需要布局的路由 */}
                <Route path="/" element={
                  <PrivateRoute>
                    <MainLayout />
                  </PrivateRoute>
                }>
                  <Route index element={<Home />} />
                  <Route path="courses" element={<Courses />} />
                  <Route path="courses/:id" element={<CourseDetail />} />

                  {/* 需要登录的路由 */}
                  <Route path="profile" element={<Profile />} />
                  <Route path="dashboard" element={<Dashboard />} />
                  <Route path="my-courses" element={<MyCourses />} />
                  
                  {/* 需要教师角色的路由 */}
                  <Route path="student-management" element={
                    <PrivateRoute roles={['teacher', 'admin', 'super_admin']}>
                      <StudentManagement />
                    </PrivateRoute>
                  } />
                  
                  {/* 需要教师角色的路由 */}
                  <Route path="lesson-replay/:courseId/:lessonId/:studentId" element={
                    <PrivateRoute roles={['teacher', 'admin', 'super_admin']}>
                      <LessonReplay />
                    </PrivateRoute>
                  } />
                  
                  {/* 默认重定向 */}
                  <Route path="*" element={<Navigate to="/" replace />} />
                </Route>
              </Routes>
            </div>
          </Router>
        </AuthProvider>
      </App>
    </ConfigProvider>
  );
}

export default Main;
